package com.registration.interceptor;

import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.dev33.satoken.util.SaResult;
import com.registration.util.JsonResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    // 全局异常拦截
    @ExceptionHandler
    public SaResult handlerException(Exception e) {
        e.printStackTrace();
        return SaResult.error(e.getMessage());
    }

    // 拦截处理权限异常
    @ExceptionHandler(NotPermissionException.class)
    public JsonResponse handlerNotPermissionException(NotPermissionException e) {
        return JsonResponse.fail(e.getCode(),"无权限操作");
    }

    // 拦截处理角色异常
    @ExceptionHandler(NotRoleException.class)
    public JsonResponse handlerNotRoleException(NotRoleException e) {
        return JsonResponse.fail(e.getCode(),"无角色操作权限");
    }
}
